package walkera.cn.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.wk.ftpserver.FtpService;
import com.wk.ftpserver.FtpTool;
import com.aivin.ftpserver.R;

import java.util.List;

public class FtpServerActivity extends Activity {
    private FtpTool ftpTool = new FtpTool();
    private Context context ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ftp);

        context = this ;
        TextView textView = findViewById(R.id.textView);
        findViewById(R.id.closeFtpServer).setOnClickListener(onClickListener);

        requestPermission();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String ip =ftpTool.getIpAddressString( );

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String msg ;
                        if(TextUtils.isEmpty(ip)){
                            msg="ip获取失败" ;
                        }else{
                            msg =   "ftp://"+ip+":2221\n" +
                                    "账号:admin\n" +
                                    "密码:12345678"  ;
                        }
                        textView.setText(msg);
                        Intent intent = new Intent(context ,FtpService.class) ;
                        intent.putExtra("userName"  , "admin") ;
                        intent.putExtra("userPwd"  , "12345678") ;
                        intent.putExtra("shareDir" ,Environment.getExternalStorageDirectory().getAbsolutePath()) ;
                        intent.putExtra("ftpPort" ,"2221") ;
                        startService(intent);
                    }
                });
            }
        }).start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(this, FtpService.class));
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK){
            return  true ;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.closeFtpServer:
                    ( (Activity) context ) .finish();
                    break;
            }
        }
    };


    private void requestPermission() {
        XXPermissions.with(this)
                // 可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                .constantRequest()
                .permission( Manifest.permission.READ_EXTERNAL_STORAGE )
                .permission(  Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {

                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if (quick) {
                            showToastMsg(   "拒绝授权将导致APP异常");
                            XXPermissions.startPermissionActivity(context, denied);
                        }
                    }
                });
    }

    private void showToastMsg(String msg){
        Toast.makeText(this ,msg ,Toast.LENGTH_SHORT).show();
    }

}
